import axios from 'axios';
import { getToken } from '@/utils/auth';
import base from '../../base.config';

axios.defaults.baseURL = `${base.baseUrl}/`;
axios.defaults.timeout = '3000';

// create an axios instance
const service = axios.create({
  baseURL: `${base.baseUrl}/`,
  timeout: '3000',
});

// 添加请求拦截器
service.interceptors.request.use(
  // config => config,
  (config) => {
    if (getToken()) {
      const a = config;
      // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
      a.headers.Authorization = `Bearer ${getToken()}`;
    }
    return config;
  },
  (error) => {
    Promise.reject(error);
  },
);

// 添加响应拦截器
service.interceptors.response.use(
  response => response.data,
  error => Promise.reject(error.response),
);

export default service;
